package com.nursery.student.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.nursery.clazz.service.ClassInfoService;
import com.nursery.kindergarten.service.KindergartenInfoService;
import com.nursery.student.entity.StudentInfo;
import com.nursery.student.entity.StudentInfoExample;
import com.nursery.student.service.StudentInfoService;

@Controller
@RequestMapping("student")
public class StudentInfoController {

	Logger logger = LoggerFactory.getLogger(StudentInfoController.class);

	@Autowired
	StudentInfoService studentInfoService;

	@Autowired
	ClassInfoService classInfoService;

	@Autowired
	KindergartenInfoService kindergartenInfoService;

	@ResponseBody
	@RequestMapping("login")
	public String login(String cardNo, String password, String callback) {
		Map<String, String> map = studentInfoService.login(cardNo, password);
		return callback + "(" + JSON.toJSONString(map) + ")";
	}

	@ResponseBody
	@RequestMapping(value = "search", produces = "application/json; charset=utf-8")
	public String search(@RequestParam Map<String, String> map, String callback) {
		StudentInfoExample example = new StudentInfoExample();
		StudentInfoExample.Criteria criteria = example.createCriteria();
		StudentInfoExample.Criteria criteria2 = example.createCriteria();
		// id
		if (StringUtils.isNotBlank(map.get("id"))) {
			criteria.andIdEqualTo(Integer.parseInt(map.get("id")));
			criteria2.andIdEqualTo(Integer.parseInt(map.get("id")));
		}
		// 证件编号
		if (StringUtils.isNotBlank(map.get("cardNo"))) {
			criteria.andCardNoEqualTo(map.get("cardNo"));
			criteria2.andCardNoEqualTo(map.get("cardNo"));
		}
		// 学生姓名
		if (StringUtils.isNotBlank(map.get("stuName"))) {
			criteria.andStuNameLike("%" + map.get("stuName") + "%");
			criteria2.andStuNameLike("%" + map.get("stuName") + "%");
		}
		// 居住地址
		if (StringUtils.isNotBlank(map.get("address"))) {
			criteria.andAddressLike("%" + map.get("address") + "%");
			criteria2.andAddressLike("%" + map.get("address") + "%");
		}
		// 监护人姓名
		if (StringUtils.isNotBlank(map.get("guarName"))) {
			criteria.andGuarName1Like("%" + map.get("guarName") + "%");
			criteria.andGuarName2Like("%" + map.get("guarName") + "%");
		}
		// 监护人证件编号
		if (StringUtils.isNotBlank(map.get("guarCardNo"))) {
			criteria.andGuarCardNo1EqualTo(map.get("guarCardNo"));
			criteria.andGuarCardNo2EqualTo(map.get("guarCardNo"));
		}
		// 幼儿园id
		if (StringUtils.isNotBlank(map.get("kindergartenId"))) {
			criteria.andKindergartenIdEqualTo(Integer.parseInt(map.get("kindergartenId")));
			criteria2.andKindergartenIdEqualTo(Integer.parseInt(map.get("kindergartenId")));
		}
		// 班级id
		if (StringUtils.isNotBlank(map.get("classId"))) {
			criteria.andClassIdEqualTo(Integer.parseInt(map.get("classId")));
			criteria2.andClassIdEqualTo(Integer.parseInt(map.get("classId")));
		}
		example.or(criteria2);
		List<StudentInfo> list = studentInfoService.selectByExample(example);
		for (StudentInfo info : list) {
			if (info.getClassId() != null) {
				info.setClassName(classInfoService.getNameByid(info.getClassId()));
			}
			if (info.getKindergartenId() != null) {
				info.setKindName(kindergartenInfoService.getNameById(info.getKindergartenId()));
			}
		}
		String json = callback + "(" + JSON.toJSONString(list) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "add", produces = "application/json; charset=utf-8")
	public String add(StudentInfo studentInfo, String callback) {
		Map<String, String> map = new HashMap<>();
		try {
			studentInfoService.add(studentInfo);
			map.put("status", "0");
		} catch (Exception e) {
			map.put("status", "1");
			logger.info(e.getMessage());
		}
		String json = callback + "(" + JSON.toJSONString(map) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "selectById", produces = "application/json; charset=utf-8")
	public String selectById(Integer id, String callback) {
		StudentInfo studentInfo = studentInfoService.selectById(id);
		String json = callback + "(" + JSON.toJSONString(studentInfo) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "update", produces = "application/json; charset=utf-8")
	public String update(StudentInfo studentInfo, String callback) {
		Map<String, String> map = new HashMap<>();
		try {
			studentInfoService.updateByPrimaryKeySelective(studentInfo);
			map.put("status", "0");
		} catch (Exception e) {
			map.put("status", "1");
			logger.info(e.getMessage());
		}
		String json = callback + "(" + JSON.toJSONString(map) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
	public String delete(Integer id, String callback) {
		Map<String, String> map = new HashMap<>();
		try {
			studentInfoService.deleteByPrimaryKey(id);
			map.put("status", "0");
		} catch (Exception e) {
			map.put("status", "1");
			logger.info(e.getMessage());
		}
		String json = callback + "(" + JSON.toJSONString(map) + ")";
		return json;
	}
}
